using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using MTRC.ODMS.DAL.COM;
using MTRC.ODMS.BLL.SOM;

namespace MTRC.ODMS.Web.SOM.Popup
{
    /// <summary>
    /// Author: huangyao
    /// This page can not to control two button right, right judgement in AR page--HZX
    /// This page inherit BasePage_SOM, this.StationEventID variable means Admission Registry Number, so need contruct an new properties for this form use--[ALStationEventID]
    /// </summary>
    public partial class PopupARAssetLoan : BasePage_SOM
    {
        /// <summary>
        /// When Page load, must set to this.ALStationEventID, No Need give value to this.StationEventID, BKZ, in this page, this.StationEventID means AR EventID
        /// </summary>
        public int ALStationEventID
        {
            get
            {
                int _aLStationEventID = -1;
                int.TryParse(string.Concat(this.ViewState["_aLStationEventID"]), out _aLStationEventID);
                return _aLStationEventID;
            }
            set
            {
                this.ViewState["_aLStationEventID"] = value;
            }
        }

        /// <summary>
        /// Issue U13:Add By Gordon 2015/05/19
        /// Set AdmissionRegistry Id To UCAssetLoan Control
        /// </summary>
        private void SetAdmRegIdToUC()
        {
            int admRegId = 0;
            int.TryParse(Request.QueryString["AdmRegId"], out admRegId);
            this.ucAssetLoan.AdmRegId = admRegId;
        }

        private void InitUCAssetLoanUserControl()
        {
            //HZX AR page must pass this QueryString Format
            //strURL = '../Popup/PopupARAssetLoan.aspx?ParentStationEventID=-1&StationEventID=-1&Action=New&IsPageAllReadOnly=1&IsOpenFromAR=1&MenuID=M140402';

            string menuIDFromQueryString = string.Concat(Request.QueryString["MenuID"]).Trim();

            int ParentStationEventID = -1;
            int.TryParse(string.Concat(Request.QueryString["ParentStationEventID"]).Trim(), out ParentStationEventID);

            int stationEventID = -1;
            int.TryParse(string.Concat(Request.QueryString["StationEventID"]).Trim(), out stationEventID);
            //if (stationEventID > 0)
            //{
            //    this.StationEventID = stationEventID;
            //}
            //this.StationEventID = stationEventID;
            this.ALStationEventID = stationEventID; //only for this page use

            string action = "";
            action = string.Concat(Request.QueryString["Action"]).Trim();

            bool IsPageAllReadOnly = false;
            if (string.Concat(Request.QueryString["IsPageAllReadOnly"]).Trim() == "1")
            {
                IsPageAllReadOnly = true;
                //and also set this page button, can not click the save button
                this.btnSaveInAR.Enabled = false;
            }

            bool IsOpenFromAR = true; //hard code to True, 
            //bool IsOpenFromAR = false;
            //if (string.Concat(Request.QueryString["ParentStationEventID"]).Trim() == "1")
            //{
            //    IsOpenFromAR = true;
            //}

            int assetLoanDataTmpID = -1;
            int.TryParse(string.Concat(Request.QueryString["AssetLoanMainDataTmpID"]).Trim(), out assetLoanDataTmpID);
            int assetLoanItemDataTmpID = -1;
            int.TryParse(string.Concat(Request.QueryString["AssetLoanItemDataTmpID"]).Trim(), out assetLoanItemDataTmpID);

            //this.ucAssetLoan.MenuID = this.MenuID;
            this.ucAssetLoan.MenuID = menuIDFromQueryString;
            //this.ucAssetLoan.StationEventID = this.StationEventID;
            this.ucAssetLoan.StationEventID = this.ALStationEventID;
            this.ucAssetLoan.ParentStationEventID = ParentStationEventID; //can use this value-- this.StationEventID;
            this.ucAssetLoan.IsPageAllReadOnly = IsPageAllReadOnly;
            this.ucAssetLoan.IsOpenFromAR = IsOpenFromAR;
            this.ucAssetLoan.AssetLoanMainDataTmpID = assetLoanDataTmpID;
            this.ucAssetLoan.AssetLoanItemDataTmpID = assetLoanItemDataTmpID;
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.ChangeLanguage += new ChangeLanguageEventHandler(ChangeLanguage_Function);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.TracePageChange = true;

            //string ss = this.txtStaffIDFromAR.Value;
            //ss = this.hdnEmployeeID.Value;

            if (!IsPostBack)
            {
                // add by dylan 20151030 for u275.1 
                this.ucAssetLoan.GridViewPageDefaultSize();

                SetAdmRegIdToUC();//Issue U13:add by Gordon 2015/05/19
                this.InitUCAssetLoanUserControl();  //can not include in --if (!IsPostBack)
                this.ucAssetLoan.UCAssetLoanPageLoadMethod();
                SetLanguage();
                Display();
                //this.ucSignature.InitUCSignatureValue(1, this.hdnSignatureFullFileName.Value); //this method had included in BindData()
            }
            else
            {
                this.IsServerChanged = true; //???
                this.AjaxUpdateIsServerChanged(); //huangyao 20081226, put all control in updatepanel
            }

            //huangyao 20090320----begin--fixed can submit more than one time when the server on slowly
            string needBindScript = "return processing(true);";
            this.btnSaveInAR.Attributes.Add("onclick", needBindScript);
            //huangyao 20090320----end--fixed can submit more than one time when the server on slowly
        }

        private void Display()
        {
            ////if AL had some returned item, can not void
            //if (this.ucAssetLoan.CanVoidAssetLoanInSeperateAL)
            //{
            //    //this.btnDelete.Attributes.Add("onclick", "return confirm('" + GetMessage("ConfirmDelete") + "');");
            //}
            //else
            //{
            //    //this.btnDelete.Attributes.Add("onclick", "alert('" + GetMessage("ConfirmDelete") + "'); return false;");
            //    string msg = "An Asset Loan can not void when there is some item had returned.";
            //    //this.btnDelete.Attributes.Add("onclick", "alert('" + msg + "'); return false;");
            //}
        }

        #region SetLanguage

        void ChangeLanguage_Function(object sender, EventArgs e)
        {
            SetLanguage();
        }

        private void SetLanguage()
        {
            //this.btnOpenChangeAssetLoanEventTime.Attributes["title"] = this.GetLabel("SOM_AmendEventTime"); //"Change Asset Loan Event Time";
            //this.btnNew.Text = this.GetLabel("COM_Add");
            //this.btnCopyAsNew.Text = this.GetLabel("SOM_CopyAsNew");
            //this.btnSave.Text = this.GetLabel("COM_Save");
            //this.btnPrint.Text = this.GetLabel("COM_Print");
            //this.btnDelete.Text = this.GetLabel("COM_Delete");

            this.btnSaveInAR.Text = this.GetLabel("SOM_Confirm"); //COM_SaveAndReturn  huangyao 20081127 change button text
                                                                      //COM_LoginButton by hzx for 20081213_SOMS_wrap_screen(1).doc ,on 2008.12.17
            this.btnCancelInAR.Text = this.GetLabel("COM_Cancel");
        }

        #endregion

        public void btnSaveInAR_Click(object sender, EventArgs e)
        {
            ucAssetLoan.IsAlert = false;//add by gordon 20160127 ISU06
            this.ucAssetLoan.SaveGrdAssetLoanItemEditRowData();
            //add by gordon 20160127 ISU06
            if (!ucAssetLoan.IsAlert)
            {
                //-------------- Add by zengshunbo 2009-02-10-------------
                if (this.ucAssetLoan.IsShowMessageInARSaveCheck())
                {
                    return;
                }
                //-------------- End add by zengshunbo 2009-02-10-------------


                if (this.ucAssetLoan.ValidationBeforeSaveOperation())
                {
                    this.ucAssetLoan.SaveDataForALInAR();
                    //Comment by gordon 20160215 ISU51
                    //update by gordon 20160201 ISU12
                    //Log.Logger("After Confirm in popup window",4, ODMUser.ID, ODMUser.UserGroupName);
                    //Log.Logger("assetLoanMainDataTmpID:" + this.ucAssetLoan.AssetLoanMainDataTmpID,4, ODMUser.ID, ODMUser.UserGroupName);
                    //Log.Logger("assetLoanItemDataTmpID:" + this.ucAssetLoan.AssetLoanItemDataTmpID,4, ODMUser.ID, ODMUser.UserGroupName);
                    //update by gordon end
                    //Comment end by gordon 20160215 ISU51
                    int assetLoanMainDataTmpID = this.ucAssetLoan.AssetLoanMainDataTmpID;
                    int assetLoanItemDataTmpID = this.ucAssetLoan.AssetLoanItemDataTmpID;

                    //-------------------- Issue U13:Add by gordon 2015/05/20
                    string assetLoanMainDataTmpIDString = assetLoanMainDataTmpID.ToString();
                    string assetLoanItemDataTmpIDString = assetLoanItemDataTmpID.ToString();
                    //-------------------- ----------------------------------

                    //huangyao added 20081124 fixed issue 0629
                    int isNeedSignAgainInAR = 0;
                    if (this.ucAssetLoan.IsNeedSignAgainInAR)
                    {
                        isNeedSignAgainInAR = 1;
                    }

                    //-------------------- Issue U13:add by gordon 2015/05/18
                    if (this.ucAssetLoan.RecordCount < 1)
                    {
                        assetLoanMainDataTmpIDString = assetLoanItemDataTmpIDString = "\"\"";
                        isNeedSignAgainInAR = 0;
                    }
                    //-------------------- -----------------------------------

                    //---------------
                    //string writeScript = "SetParentValueAndClose('" + assetLoanMainDataTmpID.ToString() + "', '" + assetLoanItemDataTmpID.ToString() + "');";
                    //Response.Write(writeScript);
                    //Return value to AR page. use javascript array
                    string returnVal = "var   arrReturn=new   Array()   ;";

                    //-------------------- Issue U13:Update gordon 2015/05/20
                    //returnVal = returnVal + " arrReturn[0]=" + assetLoanMainDataTmpID.ToString() + ";";
                    //returnVal = returnVal + " arrReturn[1]=" + assetLoanItemDataTmpID.ToString() + ";";
                    returnVal = returnVal + " arrReturn[0]=" + assetLoanMainDataTmpIDString + ";";
                    returnVal = returnVal + " arrReturn[1]=" + assetLoanItemDataTmpIDString + ";";
                    //-------------------- ----------------------------------

                    returnVal = returnVal + " arrReturn[2]=" + isNeedSignAgainInAR.ToString() + ";";  //huangyao added 20081124 fixed issue 0629
                    returnVal = returnVal + " window.returnValue=arrReturn;";
                    string writeScript = "<script>" + returnVal + "; window.close();</script>";
                    Response.Write(writeScript);
                }
                else
                {
                    //
                }
            }
        }

        public void btnCancelInAR_Click(object sender, EventArgs e)
        {
            //update by gordon 20151016 U225
            ////this.ucAssetLoan.CancelOperationInARpopup(); //no need clear temp data, BKZ. may be some one popup edit, save ance close, and popup again, and click cancel. if clear Temp Data, before edit data will be Delete, so No Need
            
            //Response.Write("<script>window.close();</script>");

            string returnVal = "var   arrReturn=new   Array()   ;";
            returnVal = returnVal + " arrReturn[0]=" + this.ucAssetLoan.AssetLoanMainDataTmpID.ToString() + ";";
            returnVal = returnVal + " arrReturn[1]=" + this.ucAssetLoan.AssetLoanItemDataTmpID.ToString() + ";";
            returnVal = returnVal + " arrReturn[2]=\"cancel\";";
            returnVal = returnVal + " window.returnValue=arrReturn;";
            string writeScript = "<script>" + returnVal + "; window.close();</script>";
            Response.Write(writeScript);
            //update end 20151016
        }
    }
}
